package com.achun.getcouponleague.ui.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.achun.getcouponleague.Bean.SelectContent;
import com.achun.getcouponleague.R;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SelectContentAdapter extends RecyclerView.Adapter<SelectContentAdapter.innerholder> {
 private  List<SelectContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean>  datalist=new ArrayList<>();
    @NonNull
    @Override
    public innerholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.select_content_item, parent, false);
        return new innerholder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull innerholder holder, int position) {
        int currentItem=position;
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onItemClickListener.onItemClick(datalist.get(currentItem));
            }
        });
        holder.setDatas(datalist.get(position));

    }

    @Override
    public int getItemCount() {
        return datalist.size();
    }

    public void setDatas( List<SelectContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean> map_data ) {
        datalist.clear();
        datalist.addAll(map_data);
        notifyDataSetChanged();

    }

    public class innerholder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_cover_view)
        public ImageView iv_cover_view;
        @BindView(R.id.tv_coupon_view)
        public TextView tv_coupon_view;
        @BindView(R.id.tv_title_view)
        public TextView tv_title_view;
        @BindView(R.id.get_coupon_buy)
        public TextView get_coupon_buy;
        @BindView(R.id.old_price_view)
        public TextView old_price_view;
        public innerholder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }

        public void setDatas(SelectContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean mapDataBean) {

            Glide.with(itemView.getContext()).load("https:"+mapDataBean.getPict_url()).into(iv_cover_view);
            tv_title_view.setText(mapDataBean.getTitle());
            if (!TextUtils.isEmpty(mapDataBean.getCoupon_click_url())){
//                有优惠信息
                tv_coupon_view.setText(mapDataBean.getCoupon_info());
                old_price_view.setText("原价：￥"+mapDataBean.getZk_final_price());
            }else {
              tv_coupon_view.setVisibility(View.GONE);
                get_coupon_buy.setVisibility(View.GONE);
                old_price_view.setText("来晚了，优惠券已领完");
            }
        }
    }
    private OnItemClickListener onItemClickListener;
    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener=onItemClickListener;
    }
    public interface OnItemClickListener{
        void onItemClick(SelectContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean bean);
    }
}
